package com.roads.web.controller.common;

import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.domain.entity.SysUser;
import com.roads.common.utils.RegexUtils;
import com.roads.common.utils.SecurityUtils;
import com.roads.common.utils.StringUtils;
import com.roads.site.utils.CardUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ValidateController
 * @Description 验证
 * @Author nhp
 * @Date 2022/10/30 0021 14:20
 **/
@RestController
@RequestMapping("/validate")
public class ValidateController extends BaseController {

    /**
     * 身份证号校验
     * 并且计算性别和年龄
     *
     * @param idNo 身份证号
     * @return 结果
     */
    @GetMapping("/validIdNo")
    public AjaxResult validIdNo(String idNo) {
        AjaxResult result = new AjaxResult();
        boolean flag = CardUtils.is18CardNo(idNo);
        result.put("result", flag);
        if (flag) {
            result.put("sex", CardUtils.judgeGender(idNo));
            result.put("age", CardUtils.countAge(idNo));
            result.put("birthday", CardUtils.extractBirth(idNo));
        }
        return result;
    }

    /**
     * 电话号码校验
     *
     * @param phone 电话号码
     * @return 结果
     */
    @GetMapping("/validPhone")
    public boolean validPhone(String phone) {
        return RegexUtils.isPhone(phone);
    }

    /**
     * 验证当前登录用户是否分配机构
     *
     * @return 结果
     */
    @GetMapping("/validUserIsInstitution")
    public AjaxResult validUserIsInstitution() {
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        if (StringUtils.isEmpty(loginUser.getInstitutionCode())) {
            return error("当前登录用户未分配项目机构，不能进行操作，请联系管理员进行设置");
        } else {
            return success();
        }
    }

}
